package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.mapper.OrderMapper;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisMessageConstant;
//import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * @program: health_paret
 * @description:
 * @author: Mr.Wang
 * @create: 2020-11-12 22:40
 **/
@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Reference
    private OrderSettingService orderSettingService;
    @Reference
    private MemberService memberService;

    @Override
    @LcnTransaction
    public Order add(Map map) {
//进行校验验证码
        String validateCode = (String) map.get("validateCode");
        String telephone = (String) map.get("telephone");
        String o = RedisUtil.get(RedisMessageConstant.SENDTYPE_ORDER + telephone);
        if (StringUtils.isBlank(o)) {
            throw new RuntimeException("验证码过期");

        } else if (StringUtils.isBlank(validateCode)) {
            throw new RuntimeException("请输入验证码");
        } else if (!o.equals(validateCode)) {
            throw new RuntimeException("验证输入错误");

        }
//查询当天预约人数是否满了
        String orderDate = (String) map.get("orderDate");
        int numberThenReservations = orderSettingService.findNumberThenReservations(orderDate);
        if (numberThenReservations <= 0) {
            throw new RuntimeException("当前预约人数满了");
        }
//查看当前会员是否已经预约过
        Member memeberByTelephone = memberService.findMemeberByTelephone(telephone);
        String setmealId = (String) map.get("setmealId");
        int mid = 0;
        if (memeberByTelephone != null) {
            int count = baseMapper.findOrderBySetmealIdAndOrderDateAndMemberId(Integer.parseInt(setmealId), orderDate, memeberByTelephone.getId());
            mid = memeberByTelephone.getId();
            if (count >= 1) {
                throw new RuntimeException("已经预约过，请择日再约");
            }
        } else {
//没查到的话就更新会员表
            //  更新会员表
            Member member = new Member();
            member.setName((String) map.get("name"));
            member.setSex((String) map.get("sex"));
            member.setIdCard((String) map.get("idCard"));
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date(System.currentTimeMillis()));
            mid = memberService.saveMember(member);
        }

        //通过以上的校验可以进行更新表了
        orderSettingService.updateReservationByOrderDate(orderDate);
        Order order = new Order();
        order.setMemberId(mid);
        order.setOrderDate(DateUtils.parseString2Date(orderDate, "yyyy-MM-dd"));
        order.setOrderType((String) map.get("orderType"));
        order.setOrderStatus("未到诊");
        order.setSetmealId(Integer.parseInt(setmealId));
        save(order);
        return order;
    }

    @Override
    public Map findOrderInfoByOrderId(int id) {
        Map x = baseMapper.findOrderInfoByOrderId(id);
        return x;
    }
}
